#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
#define int long long
using namespace std;

signed main()
{
    int t; cin >> t;
    while (t--)
    {
        int l, r; cin >> l >> r;
        cout << r * (r + 1) << endl;
        vector<int> arr(r - l + 1);
        for (int i = 0; i < r - l + 1; ++i) arr[i] = i;
        while (true)
        {
            int i = 0;
            while (i < r) i = (i << 1) | 1;
            int tgt = i - r;
            //cout << i << ' ' << tgt << endl;
            reverse(arr.begin() + tgt, arr.begin() + r + 1);
            r = tgt - 1;
            if (r <= 0) break;
        }
        for (auto& e : arr) cout << e << ' '; cout << endl;
    }
    return 0;
}
